﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using LayerDTO;

namespace LayerDAO
{
    public class LoaiDAO
    {
        public List<LoaiDTO> LayDanhSachLoaiMH(string connect)
        {
            DataTable dt = new DataTable();
            DataProvider _provider = new DataProvider(connect);
            if (_provider.OpenConnect())
                dt = _provider.SqlExcuteQuery("LayDanhSachLoaiMatHang", null);
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            List<LoaiDTO> list = new List<LoaiDTO>();
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    LoaiDTO loaiMH = new LoaiDTO();
                    loaiMH.MaLoaiMH = dt.Rows[i]["MLMH"].ToString();
                    loaiMH.TenLoaiMH = dt.Rows[i]["TenLoaiMH"].ToString();
                    loaiMH.DaXoa = (bool)dt.Rows[i]["DaXoa"];
                    list.Add(loaiMH);
                }
            }
            return list;
        }
        public string ThemLoaiMH(LoaiDTO loaiMH, string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("ThemLoaiMatHang", new SqlParameter("@tenMH", loaiMH.TenLoaiMH),par);

            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }

        public string SuaLoaiMatHang(LoaiDTO loaiMH, string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("SuaLoaiMatHang", new SqlParameter("@maLoai", loaiMH.MaLoaiMH),
                                                              new SqlParameter("@tenLoai", loaiMH.TenLoaiMH),
                                                           
                                                              new SqlParameter("@daXoa", loaiMH.DaXoa), par);

            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }
        public string XoaLoaiMatHang(LoaiDTO loaiMH, string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("XoaLoaiMatHang", new SqlParameter("@maLoai", loaiMH.MaLoaiMH), par);


            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }
    }
}